package com.mbds.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.example.sismapp.R;

public class MainActivity extends ActionBarActivity {
	private static String LOGIN_KEY_STRING;
	private static String STOP_DB_KEY;
	private static String LOCATION_KEY;
	
	private EditText login;
	
	public static String getLOGIN_KEY_STRING() {
		return LOGIN_KEY_STRING;
	}

	public static String getSTOP_DB_KEY() {
		return STOP_DB_KEY;
	}
	
	public static String getLOCATION_KEY() {
		return LOCATION_KEY;
	}

	public void startButton(View view){
		
		AlertDialog.Builder alertLog = new AlertDialog.Builder(this);

		String logText = login.getText().toString();
		if(logText.equals("")){
			
			alertLog.setMessage(R.string.dialog_errLog)
            .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                   
                }
            });
			alertLog.create();
			alertLog.show();
		}else{
			Intent map = new Intent(this, MapActivity.class);
			//Save the login in the preference
			SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
			sharedPref.edit().putString(LOGIN_KEY_STRING, logText).commit();
			
			startActivity(map);
		}
	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        login = (EditText) this.findViewById(R.id.editLogin);
        
        PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);
        
       
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	//Get the key of some preferences
    	LOGIN_KEY_STRING = getString(R.string.pref_key_login);
    	STOP_DB_KEY = getString(R.string.pref_key_start_db);
    	LOCATION_KEY = getString(R.string.pref_key_location);
    	
    	//Check if the preference has been modified
    	 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
         String defaultLogin = sharedPref.getString(LOGIN_KEY_STRING, "");
         login = (EditText) this.findViewById(R.id.editLogin);
         if(!defaultLogin.equals("Login")) {
         	login.setText(defaultLogin);
         }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    	

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            
            return rootView;
        }
    }

}
